package com.ymk;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.utlis.Aes;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.user.MchUserBO;
import com.ymk.core.code.cmd.SessionCmdCode;
import com.ymk.core.code.cmd.UserCmdCode;
import com.ymk.core.constants.SysConstants;
import com.ymk.core.enums.sop.SopUserStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Service
public class MchSessionPostCmd extends ACmd {

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        MchUserBO reqData = super.getData(sign, MchUserBO.class);

        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("mobile", reqData.getMobile());

        CmdSign mchGetCmd = CmdSign.Builder.newBuild(sign)
                .setCmdCode(UserCmdCode.MCH_USER_GET)
                .setSource(paramsBuilder.build())
                .setScope(SysConstants.CMD_SERVER_SCOPE)
                .build();
        CmdSign mchGetSign = this.scheduler.schedule(request, mchGetCmd);
        MchUserBO dbMchUserBO = super.getData(mchGetSign, MchUserBO.class);

        if (StringUtils.isBlank(dbMchUserBO.getMobile())) {
            throw new MallException("用户不存在");
        }

        if (dbMchUserBO.getStatus().equals(SopUserStatusEnum.DISABLED.getStatus())) {
            throw new MallException("账号已禁用");
        }

        if (!Aes.getMD5(reqData.getPassword(), dbMchUserBO.getSalt()).equals(dbMchUserBO.getPassword())) {
            throw new MallException("密码错误");
        }

        // 设置权限
        String token = IdUtil.simpleUUID();
        dbMchUserBO.setPassword("");
        dbMchUserBO.setSalt("");
        this.redisTemplate.opsForValue().set(SysConstants.REDIS_KEY_JWT + token, JSON.toJSONString(dbMchUserBO), 60 * 60 * 4);

        paramsBuilder.clearParam()
                .addParam("token", token);

        respSign.setSource(super.getSource(paramsBuilder.build()));

        return respSign;
    }

    @Override
    public CmdSign verify(HttpServletRequest request, CmdSign sign) throws Exception {
        MchUserBO mchUserBO = super.getData(sign, MchUserBO.class);
        if (StringUtils.isBlank(mchUserBO.getMobile())) {
            throw new MallException("手机号为空");
        }
        if (StringUtils.isBlank(mchUserBO.getPassword())) {
            throw new MallException("密码为空");
        }
        return null;
    }

    @Override
    public boolean anon(CmdSign sign) {
        return true;
    }

    @Override
    public String getCmdCode() {
        return SessionCmdCode.MCH_MEMBER_POST;
    }

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
}
